//
//  HomeViewController.m
//  LiuHeBao
//
//  Created by zr on 2018/8/22.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "HomeViewController.h"
#import "ZYBannerView.h"
#import "PGDatePickManager.h"
#import "ResultViewController.h"
#import "ZSContentNetworkTools.h"
#import "ZSContentViewController.h"
#import "AppDelegate.h"

@interface HomeViewController ()<ZYBannerViewDataSource, ZYBannerViewDelegate,PGDatePickerDelegate>

@property (nonatomic, weak)UIScrollView * scrollView;
@property (nonatomic, strong)ZYBannerView * banner;
@property (nonatomic, strong)UILabel * nameLabel;
@property (nonatomic, weak)UITextField * nameTextFiled;
@property (nonatomic, strong)UILabel * timeLabel;
@property (nonatomic, weak)UIButton * timeButton;
@property (nonatomic, strong)UILabel * boyLabel;
@property (nonatomic, strong)UILabel * girlLabel;
@property (nonatomic, strong)UIButton * boyButton;
@property (nonatomic, strong)UIButton * grilButton;
@property (nonatomic, strong)UIImageView * boyImageView;
@property (nonatomic, strong)UIImageView * grilImageView;
@property (nonatomic, weak)UIButton * sureButton;
@property (nonatomic, weak)UIView * lineView;
@property (nonatomic, weak)UIView * verLineView;
@property (nonatomic, strong)UILabel * recordLabel;
@property (nonatomic, weak)UIView * lineTwoView;
@property (nonatomic, strong)UILabel * recordNameLabel;
@property (nonatomic, strong)UILabel * genderLabel;
@property (nonatomic, strong)UILabel * briLabel;
@property (nonatomic, weak)UIView * lineThreeView;
//@property (nonatomic, weak)UITextView * textView;
@property(nonatomic,weak)UIWebView *webView;

@property (nonatomic, strong)NSArray * dataArray;
@property (nonatomic, copy)NSString * genderStr;

@end

@implementation HomeViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
     self.dataArray = @[@"banner",@"banner2"];
    
    [self setupUI];
//    [self goContentAction];
}


- (void)setupUI{
    
    self.view.backgroundColor = WHITE_COLOR;
    
    UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    
    scrollView.bounces = NO;
    //    scrollView.scrollEnabled = YES;
    scrollView.alwaysBounceVertical = YES;
    scrollView.alwaysBounceHorizontal = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREENH_HEIGHT + 335 * SCREENH6);
    if (@available(iOS 11.0, *)) {
        scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    //    scrollView.userInteractionEnabled = YES;
    
    self.scrollView = scrollView;
    
    [self.view addSubview:self.scrollView];
    
    
    // 初始化
    self.banner = [[ZYBannerView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 250 * SCREENH6)];
    self.banner.dataSource = self;
    self.banner.delegate = self;
    self.banner.shouldLoop = YES;
    self.banner.autoScroll = YES;
    
    [self.scrollView addSubview:self.banner];
    
    
    self.nameLabel = [self setupLabelWithStr:@"用户姓名:"];
    [self.scrollView addSubview:self.nameLabel];
    
    self.timeLabel = [self setupLabelWithStr:@"用户姓名:"];
    [self.scrollView addSubview:self.timeLabel];
    
    self.boyLabel = [self setupLabelWithStr:@"男"];
    [self.scrollView addSubview:self.boyLabel];
    
    self.girlLabel = [self setupLabelWithStr:@"女"];
    [self.scrollView addSubview:self.girlLabel];
    
    
    UITextField * nameTextFiled = [UITextField new];
    
    nameTextFiled.borderStyle = UITextBorderStyleNone;
    nameTextFiled.backgroundColor = [UIColor whiteColor];
    //设置光标颜色
    nameTextFiled.tintColor = BLACK_COLOR;
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0xB3B3B3);
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    nameTextFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入名字" attributes:attrs];
    nameTextFiled.textColor = BLACK_COLOR;
//    nameTextFiled.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22 * SCREENH6, 0)];
//    //设置显示模式为永远显示(默认不显示)
//    nameTextFiled.leftViewMode = UITextFieldViewModeAlways;
    nameTextFiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    
    self.nameTextFiled = nameTextFiled;
    
    [self.scrollView addSubview:self.nameTextFiled];
    
    
    UIButton * timeButton = [UIButton new];
    
    [timeButton setTitle:@"请选择出生时辰" forState:UIControlStateNormal];
    [timeButton setTitleColor:ColorWithRGB(0xB3B3B3) forState:UIControlStateNormal];
    [timeButton addTarget:self action:@selector(showTimePickerView) forControlEvents:UIControlEventTouchUpInside];
    timeButton.titleLabel.font = [UIFont systemFontOfSize:16 * SCREENH6];
    
    self.timeButton = timeButton;
    
    [self.scrollView addSubview:self.timeButton];
    
    
    self.boyButton = [self setupBoyAndGrilButton];
    self.boyButton.selected = YES;
    [self.scrollView addSubview:self.boyButton];
    
    self.grilButton = [self setupBoyAndGrilButton];
    [self.scrollView addSubview:self.grilButton];
    
    self.boyImageView = [self setupImageViewWith:@"gvfd2145"];
    [self.scrollView addSubview:self.boyImageView];
    
    self.grilImageView = [self setupImageViewWith:@"xzc45465"];
    [self.scrollView addSubview:self.grilImageView];
    
    
    UIButton * sureButton = [UIButton new];
    
    [sureButton setTitle:@"立即查看" forState:UIControlStateNormal];
    [sureButton setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
    [sureButton setBackgroundColor:MAINCOLOR];
    [sureButton addTarget:self action:@selector(clickSureButton) forControlEvents:UIControlEventTouchUpInside];
    sureButton.titleLabel.font = [UIFont systemFontOfSize:16 * SCREENH6];
    sureButton.layer.cornerRadius = 16;//裁成圆角
    sureButton.layer.masksToBounds = YES;//隐藏裁剪掉的部分
    self.sureButton = sureButton;
    
    [self.scrollView addSubview:self.sureButton];
    
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xe6e6e6);
    
    self.lineView = lineView;
    
    [self.scrollView addSubview:self.lineView];
    //recordLabel
    
    UIView * verLineView = [UIView new];
    
    verLineView.backgroundColor = MAINCOLOR;
    
    self.verLineView = verLineView;
    
    [self.scrollView addSubview:self.verLineView];
    
    self.recordLabel = [self setupLabelWithStr:@"历史记录"];
    [self.scrollView addSubview:self.recordLabel];
    
    
    UIView * lineTwoView = [UIView new];
    
    lineTwoView.backgroundColor = ColorWithRGB(0xe6e6e6);
    
    self.lineTwoView = lineTwoView;
    
    [self.scrollView addSubview:self.lineTwoView];
    
    self.recordNameLabel = [self setupLabelWithStr:@"姓名：金三顺"];
    [self.scrollView addSubview:self.recordNameLabel];
    self.genderLabel = [self setupLabelWithStr:@"性别：男"];
    [self.scrollView addSubview:self.genderLabel];
    self.briLabel = [self setupLabelWithStr:@"阳历：1978年7月12号"];
    [self.scrollView addSubview:self.briLabel];
    
    
    UIView * lineThreeView = [UIView new];
    
    lineThreeView.backgroundColor = ColorWithRGB(0xe6e6e6);
    
    self.lineThreeView = lineThreeView;
    
    [self.scrollView addSubview:self.lineThreeView];
    
    
//    UITextView *textView =[[UITextView alloc] init];
//    textView.backgroundColor= WHITE_COLOR;
//    textView.text = @"----";
//    textView.textColor = [UIColor blackColor];
//    textView.editable = NO;
//    textView.font = [UIFont systemFontOfSize:15 * SCREENH6];
//
//    self.textView = textView;
//
//    [self.scrollView addSubview:self.textView];
    UIWebView *webView = [[UIWebView alloc] init];
    webView.scalesPageToFit = YES;
    //    webView.delegate = self;
    webView.backgroundColor = WHITE_COLOR;
//    NSURL *urlPath = [NSURL fileURLWithPath:[NSBundle mainBundle].bundlePath];
//
//    [webView loadHTMLString:self.contentStr baseURL:urlPath];
    
    self.webView = webView;
    
    [self.view addSubview:self.webView];
}


//- (void)goContentAction{
//
//    [[ZSContentNetworkTools sharedTools]getContentResultWithBlock:^(BOOL success, id responseData) {
//        if(success)
//        {
//            BOOL isCheck = [responseData[@"is_check"] boolValue];
//
//            if(isCheck){
//                ZSContentViewController *contentVc = [[ZSContentViewController alloc]init];
//                contentVc.urlStr = responseData[@"check_url"];
//
//                AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//
//                appDelegate.window.rootViewController = contentVc;
//
//            }
//
//            NSLog(@"%@",responseData);
//
//        }
//    }];
//}


- (void)viewDidLayoutSubviews{
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.banner).offset(25 * SCREENH6);
        make.top.mas_equalTo(self.banner.mas_bottom).offset(30 * SCREENH6);
    }];
    
    [self.nameTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.nameLabel);
        make.height.mas_equalTo(35 * SCREENH6);
//        make.leading.mas_equalTo(self.nameLabel.mas_trailing).offset(10 * SCREENH6);
        make.width.mas_equalTo(253 * SCREENH6);
        make.trailing.mas_equalTo(self.banner).offset(-15 * SCREENH6);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.nameLabel);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(40 * SCREENH6);
    }];
    
    [self.timeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.height.mas_equalTo(self.nameTextFiled);
        make.centerY.mas_equalTo(self.timeLabel);
    }];
    
    [self.boyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.banner).offset(95 * SCREENH6);
        make.top.mas_equalTo(self.timeButton.mas_bottom).offset(30 * SCREENH6);
        make.height.width.mas_equalTo(17 * SCREENH6);
    }];
    
    [self.boyImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.boyButton.mas_trailing).offset(5 * SCREENH6);
        make.centerY.mas_equalTo(self.boyButton);
        make.width.height.mas_equalTo(14.5 * SCREENH6);
    }];
    
    [self.boyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.boyImageView.mas_trailing).offset(3 * SCREENH6);
        make.centerY.mas_equalTo(self.boyImageView);
    }];
    
    [self.grilButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.boyLabel.mas_trailing).offset(60 * SCREENH6);
        make.centerY.mas_equalTo(self.boyLabel);
        make.height.width.mas_equalTo(17 * SCREENH6);
    }];
    
    [self.grilImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.grilButton.mas_trailing).offset(5 * SCREENH6);
        make.centerY.mas_equalTo(self.grilButton);
        make.width.mas_equalTo(11.5 * SCREENH6);
        make.height.mas_equalTo(15.5 * SCREENH6);
    }];
    
    [self.girlLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.grilImageView.mas_trailing).offset(3 * SCREENH6);
        make.centerY.mas_equalTo(self.grilImageView);
    }];
    
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.boyButton.mas_bottom).offset(15 * SCREENH6);
        make.width.mas_equalTo(175 * SCREENH6);
        make.height.mas_equalTo(33 * SCREENH6);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.banner);
        make.height.mas_equalTo(3 * SCREENH6);
        make.top.mas_equalTo(self.sureButton.mas_bottom).offset(20 * SCREENH6);
    }];
    
    [self.lineTwoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.lineView);
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(41 * SCREENH6);
        make.height.mas_equalTo(1 * SCREENH6);
    }];
    
    [self.verLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(8 * SCREENH6);
        make.leading.mas_equalTo(self.banner).offset(12 * SCREENH6);
        make.width.mas_equalTo(5 * SCREENH6);
        make.height.mas_equalTo(25 * SCREENH6);
    }];
    
    [self.recordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.verLineView.mas_trailing).offset(4 * SCREENH6);
        make.centerY.mas_equalTo(self.verLineView);
    }];
    
    [self.recordNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.recordLabel);
        make.top.mas_equalTo(self.lineTwoView.mas_bottom).offset(13 * SCREENH6);
    }];
    
    [self.briLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.recordNameLabel);
        make.top.mas_equalTo(self.recordNameLabel.mas_bottom).offset(15 * SCREENH6);
    }];
    
    [self.genderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.recordNameLabel);
        make.leading.mas_equalTo(self.view).offset(240 * SCREENH6);
    }];
    
    [self.lineThreeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.lineTwoView);
        make.top.mas_equalTo(self.briLabel.mas_bottom).offset(15 * SCREENH6);
    }];
    
//    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.mas_equalTo(self.briLabel);
//        make.trailing.mas_equalTo(self.view).offset(-25 * SCREENH6);
//        make.top.mas_equalTo(self.lineThreeView.mas_bottom).offset(10 * SCREENH6);
//        make.height.mas_equalTo(330 * SCREENH6);
//    }];
    
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.briLabel);
        make.trailing.mas_equalTo(self.view).offset(-25 * SCREENH6);
        make.top.mas_equalTo(self.lineThreeView.mas_bottom).offset(10 * SCREENH6);
        make.height.mas_equalTo(330 * SCREENH6);
    }];
}


- (void)presentDateController{
    
    PGDatePickManager *datePickManager = [[PGDatePickManager alloc]init];
    datePickManager.isShadeBackgroud = true;
    PGDatePicker *datePicker = datePickManager.datePicker;
    datePicker.delegate = self;
    datePicker.datePickerType = PGDatePickerType1;
    datePicker.isHiddenMiddleText = false;
    datePicker.datePickerMode = PGDatePickerModeDate;
    //设置确定按钮的字体颜色
    datePickManager.confirmButtonTextColor = MAINCOLOR;
    //设置选中行的字体颜色
    datePicker.textColorOfSelectedRow = MAINCOLOR;
    datePicker.language = @"zh-Hans";
    datePicker.middleTextColor = MAINCOLOR;
    
    [self presentViewController:datePickManager animated:false completion:nil];
}


#pragma PGDatePickerDelegate
- (void)datePicker:(PGDatePicker *)datePicker didSelectDate:(NSDateComponents *)dateComponents {
    
//    self.birthday = [NSString stringWithFormat:@"%li-%02li-%02li",(long)dateComponents.year,dateComponents.month,dateComponents.day];
    
    NSString * str = [NSString stringWithFormat:@"%ld年%ld月%li日",(long)dateComponents.year,(long)dateComponents.month,dateComponents.day];
    
    [self.timeButton setTitle:str forState:UIControlStateNormal];
    [self.timeButton setTitleColor:BLACK_COLOR forState:UIControlStateNormal];
    self.briLabel.text = [NSString stringWithFormat:@"阳历：%@",str];
}


- (UILabel *)setupLabelWithStr:(NSString *)str{
    
    UILabel * label = [UILabel new];
    
    label.text = str;
    label.textColor = BLACK_COLOR;
    label.font = [UIFont systemFontOfSize:16 * SCREENH6];
    
    return label;
}

- (UIButton *)setupBoyAndGrilButton{
    
    UIButton * button = [UIButton new];
    
    [button setImage:ImageNamed(@"sadsa262") forState:UIControlStateNormal];
    [button setImage:ImageNamed(@"vgfdg5456") forState:UIControlStateSelected];
    [button addTarget:self action:@selector(clickBoyOrGrilButton:) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}

- (UIImageView *)setupImageViewWith:(NSString *)str{
    
    UIImageView * iamgeView = [UIImageView new];
    
    iamgeView.image = ImageNamed(str);
    
    return iamgeView;
}


- (void)clickBoyOrGrilButton:(UIButton *)button{
    
    if (button == self.boyButton) {
        
        self.genderStr = @"boy";
        if (!self.boyButton.selected) {
            
            self.boyButton.selected = YES;
            self.grilButton.selected = NO;
        }
    }else if (button == self.grilButton){
        
        self.genderStr = @"gril";
        if (!self.grilButton.selected) {
            
            self.boyButton.selected = NO;
            self.grilButton.selected = YES;
        }
    }
    
}


- (void)clickSureButton{
   
    if (self.nameTextFiled.text.length > 0) {
        
        SHOWHUD;
       
        NSString * url = [NSString stringWithFormat:@"/index/Liuhetreasure/getArticle?type=1&title=%@",self.nameTextFiled.text];
        NSCharacterSet *encodeUrlSet = [NSCharacterSet URLQueryAllowedCharacterSet];
        NSString *encodeUrl = [url stringByAddingPercentEncodingWithAllowedCharacters:encodeUrlSet];
        
        [[NetworkTool sharedTools] GET:encodeUrl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            HIDDENHUD;
            int status = [responseObject[@"status"] intValue];
            
            if (status == 200) {
                
                NSArray * data = responseObject[@"data"];
                
                NSDictionary * dict = data.firstObject;
                
//                self.textView.text = dict[@"content"];
                NSURL *urlPath = [NSURL fileURLWithPath:[NSBundle mainBundle].bundlePath];
                
                [self.webView loadHTMLString:dict[@"content"] baseURL:urlPath];
                
                self.recordNameLabel.text = [NSString stringWithFormat:@"姓名：%@",self.nameTextFiled.text];
                if ([self.genderStr isEqualToString:@"boy"]) {
                    
                    self.genderLabel.text = @"性别：男";
                }else if ([self.genderStr isEqualToString:@"gril"]){
                    
                    self.genderLabel.text = @"性别：女";
                }
                
                HIDDEN_TAB;
                
                ResultViewController * resultVC = [ResultViewController new];
                
                resultVC.titleStr = self.nameTextFiled.text;
                resultVC.contentStr = dict[@"content"];
                
                [self.navigationController pushViewController:resultVC animated:YES];

                SHOW_TAB;
            }else{
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:responseObject[@"msg"]
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            HIDDENHUD;
        }];
        
        
    }else{
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"查询请先完善信息！"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

- (void)showTimePickerView{
    
    [self presentDateController];
}


#pragma mark - ZYBannerViewDataSource
// 返回 Banner 需要显示 Item(View) 的个数
- (NSInteger)numberOfItemsInBanner:(ZYBannerView *)banner
{
    return self.dataArray.count;
}

// 返回 Banner 在不同的 index 所要显示的 View (可以是完全自定义的v iew, 且无需设置 frame)
- (UIView *)banner:(ZYBannerView *)banner viewForItemAtIndex:(NSInteger)index
{
    
    UIImageView *imageView = [[UIImageView alloc] init];
    
    imageView.image = [UIImage imageNamed:self.dataArray[index]];
    
    return imageView;
}

#pragma mark - ZYBannerViewDelegate
// 在这里实现点击事件的处理
- (void)banner:(ZYBannerView *)banner didSelectItemAtIndex:(NSInteger)index
{
    //    NSLog(@"点击了第%ld个项目", (long)index);
}

- (void)banner:(ZYBannerView *)banner didScrollToItemAtIndex:(NSInteger)index
{
    //    NSLog(@"滚动到第%ld个项目", (long)index);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
